<template>
  <div class="app-container">
    <div class="title-btns">
      <span class="role-span"><span></span>代码信息列表</span>
      <div style="float: right">
        <!-- 新增 -->
        <el-button class="filter-item" size="mini" type="primary" icon="el-icon-plus" @click="toAdd">新增</el-button>
        <el-button v-show="clildsFlag" class="filter-item" size="mini" type="warning" icon="el-icon-arrow-left" @click="toback">返回</el-button>
      </div>
    </div>
    <div class="head-container">
      <div class="head-search">
        <el-input v-model="query.className" clearable placeholder="请输入实体类名称" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery" />
        <el-button class="filter-item" size="mini" type="primary" icon="el-icon-search" @click="toQuery">搜索</el-button>
        <el-button class="filter-item" size="mini" icon="el-icon-refresh" @click="toReSet">重置</el-button>
      </div>
      <!--表格渲染-->
      <el-table v-show="!clildsFlag" v-loading="loading" :data="data" size="small" style="width: 100%;">
        <el-table-column label="序号" width="80" align="center">
          <template slot-scope="scope">
            <div>{{ scope.$index + 1 }}</div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="name" label="表名" />
        <el-table-column :show-overflow-tooltip="true" prop="className" label="实体类名" />
        <el-table-column :show-overflow-tooltip="true" prop="comments" label="表描述" />
        <el-table-column prop="childsNum" label="子表数量">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="toChilds(scope.row.id)" v-text="scope.row.childsNum" />
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="createBy" label="创建者" />
        <el-table-column label="操作" width="340px" align="center">
          <template slot-scope="scope">
            <el-button v-permission="['ADMIN']" size="mini" type="primary" @click="toUpdate(scope.row)">基础</el-button>
            <el-button v-permission="['ADMIN']" size="mini" type="primary" @click="toColumn(scope.row.id)">字段</el-button>
            <el-button v-permission="['ADMIN']" size="mini" type="primary" @click="toScheme(scope.row.id)">生成</el-button>

            <el-popover
              :ref="scope.row.id"
              v-permission="['ADMIN']"
              placement="top"
              width="200"
            >
              <p>确定删除吗,如果存在下级节点则一并删除，此操作不能撤销！</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
                <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
              </div>
              <el-button slot="reference" type="danger" style="margin-left:10px" size="mini">删除</el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>

      <el-table v-show="clildsFlag" v-loading="loading" :data="data" size="small" style="width: 100%;">
        <el-table-column label="序号" width="80" align="center">
          <template slot-scope="scope">
            <div>{{ scope.$index + 1 }}</div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="name" label="表名" />
        <el-table-column :show-overflow-tooltip="true" prop="className" label="实体类名" />
        <el-table-column :show-overflow-tooltip="true" prop="comments" label="表描述" />
        <el-table-column prop="createDate" label="创建日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="createBy" label="创建者" />
        <el-table-column label="操作" width="300px" align="center">
          <template slot-scope="scope">
            <el-button v-permission="['ADMIN']" size="mini" type="text" @click="toUpdate(scope.row)">基础</el-button>
            <el-button v-permission="['ADMIN']" size="mini" type="text" @click="toColumn(scope.row.id)">字段</el-button>
            <el-popover
              :ref="'z'+scope.row.id"
              v-permission="['ADMIN']"
              placement="top"
              width="200"
            >
              <p>确定删除吗,如果存在下级节点则一并删除，此操作不能撤销！</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="$refs['z'+scope.row.id].doClose()">取消</el-button>
                <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
              </div>
              <el-button slot="reference" type="text" style="margin-left:10px" size="mini">删除</el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        :total="total"
        :current-page="page"
        style="margin-top: 8px;"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="sizeChange"
        @current-change="pageChange"
      />
    </div>
    <eForm ref="form" :is-add="isAdd" />
    <scheme ref="scheme" :dictobj="dictobj" />
    <column ref="column" :dicts="dicts" />
  </div>
</template>

<script>
import initData from '@/mixins/initData'
import initDict from '@/mixins/initDict'
import { parseTime } from '@/utils/index'
import eForm from './module/form'
import { del } from '@/api/generator/table'
import scheme from './module/scheme'
import column from './module/column'
import { getDicts } from '@/api/system/dict'
export default {
  components: { scheme, eForm, column },
  mixins: [initData, initDict],
  data() {
    return {
      clildsFlag: false,
      dataClilds: [],
      parentId: null,
      delLoading: false
    }
  },
  created() {
    this.getDictObj('gen_category')
    this.initDicts()
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    beforeInit() {
      this.url = 'api-generator/table'
      const query = this.query
      const name = query.className
      this.params = { page: this.page, size: this.size }
      if (name) { this.params['className'] = name }
      if (this.parentId) {
        this.params['parentId'] = this.parentId
      }
      return true
    },

    /**
     * 新增
     */
    toAdd() {
      this.isAdd = true
      const _this = this.$refs.form
      _this.resetForm()
      if (this.clildsFlag) { _this.form.parentTableFk = this.parentId }
      _this.dialog = true
    },
    /**
     * 去修改
     */
    toUpdate(data) {
      this.isAdd = false
      const _this = this.$refs.form
      _this.resetForm()
      _this.form = { name: data.name, className: data.className, comments: data.comments, remarks: data.remarks, parentTableFk: data.parentTableFk, id: data.id }
      _this.dialog = true
    },
    /**
     * 去设置字段
     */
    toColumn(id) {
      this.$refs.column.id = id
      this.$refs.column.to()
    },
    /**
     * 去设置生成信息
     */
    toScheme(id) {
      this.$refs.scheme.id = id
      this.$refs.scheme.to()
    },

    /**
 * 去子表
 */
    toChilds(id) {
      this.clildsFlag = true
      this.parentId = id
      this.page = 0
      this.init()
    },
    /**
     * 删除
     */
    subDelete(id) {
      del(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      })
    },
    /**
     * 获取所有表字典
     */
    initDicts() {
      getDicts({ sort: 'id desc' }).then(res => {
        this.dicts = res.data.page.data.map(dict => {
          return { key: dict.name, value: dict.remark }
        })
      })
    },
    // 返回
    toback() {
      this.clildsFlag = false
      this.parentId = null
      this.init()
    }
  }

}
</script>

<style scoped>

</style>
